package submit;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import wellsim.no.ntnu.fp.su.Flow;
import wellsim.no.ntnu.fp.su.Well;
import wellsim.no.ntnu.fp.su.sensors.ErosionProbe;


public class skriveTilDb {
	
	private static Db  dbm = Db.getInstance();
	
	private double T1;
	private double P1;
	private boolean update = false;
	private double rDelta;
	private Timestamp timestamp;

	private static void registrerWell(java.awt.event.ActionEvent evt) throws SQLException{
		Db dbm = Db.getInstance();
		int wellId = WellReg.getWellId(); 
		double i=0.0;
   	   	//double temperature = Flow.getTemperature(i);
   	   	//double sandContent = Flow.getSandContent();
   	   	//double pressure = Flow.getPressure();
   	   	//double velocity = Flow.getVelocity();
		//String query="INSERT INTO flow (wellId, temperature, sandContent, pressure, velocity) values ("+wellId+","+temperature + ", "+sandContent+", "+pressure+", "+velocity+");";
		//dbm.executeUpdate(query);
		//System.out.println("Vellykket skriving til databasen");
	}
	
	private void sampleWell(Well well) throws SQLException {
		
		ArrayList<Double> deltaR = new ArrayList<Double>();
		ArrayList<Double> endring = new ArrayList<Double>();
		
		double x,y;
		int teller=0;
		
		while(true){
			while(true){
				if (System.currentTimeMillis() % 1000 == 0) break;
			}
		Db dbm = Db.getInstance();
			int wellId = WellReg.getWellId();
		double[] readings = well.sample();
		//for(int i=0; i<4; i++){
			double R0 = readings[0];
			double R1 = readings[1];
			setP1(readings[2]);
			setT1(readings[3]);
			System.out.println("R0 = "+readings[0]);
			System.out.println("R1 = "+ readings[1]);
			System.out.println("P1 = "+ readings[2]);
			System.out.println("T1 = "+ readings[3]);
			
			//String query ="INSERT INTO SampleWell(wellId,R0, R1, P1, T1) values ("+wellId+","+R0+","+R1+","+P1+","+T1+");";
			//dbm.executeUpdate(query);
			update = true;
		//}
		System.out.println("---");
		
		//ArrayList<Double> deltaR = new ArrayList<Double>();
		
			//double temp = R0;
			//double y = deltaR.get(deltaR.size()-1);
		
		if(System.currentTimeMillis() % 1000 == 0) break;
		if(update = true){
			deltaR.add((double)R0);
			x = deltaR.get(deltaR.size()-1);
			System.out.println(x+"siste");
			teller = teller+1;
			if(teller>1){
				y=deltaR.get(deltaR.size()-2);
				rDelta = y-x;
				endring.add((double)rDelta);
				System.out.println(y+"nest siste");
				System.out.println(rDelta+"delta");
				
				String query2 = "INSERT INTO DeltaR(deltaRId, wellId, r, deltaR, t, p) values ("+0+","+0+","+0+","+rDelta+","+0+","+0+");";
				dbm.executeUpdate(query2);
				update = false;
			}
		}
			//double y = deltaR.get(deltaR.size()-2);
			
			//if(deltaR.size()<deltaR.size()+1){
				//double temp = x;
			//	System.out.println("nest siste" + temp);
			//}
			System.out.println("liste :" + deltaR);
			//System.out.println("siste?"+x);
			//System.out.println("nest siste" + y);
		
		
		}
		
	}
	public void setP1(double p){
		P1 = p;
	}
	public double setP1(){
		return P1;
	}
	
	public void setT1(double t){
		T1 = t; 
	}
	public double getT1(){
		return T1;
	}
	
	public void setdeltaR(double sdp){
		rDelta = sdp;
	}
	
	public double getdeltaR(){
		return rDelta;
	}
	
	public void setTimestamp(Timestamp timestamp){
		this.timestamp = timestamp;
	}
	
	public Timestamp getTimestamp(){
		return timestamp;
	}
	
	//public static ResultSet getUpdates() throws SQLException{
		
		//String query = "SELECT R0 FROM SampleWell"; 
		//return  dbm.executeQuery(query);
	//}
	
	
	public static void main(String args[]) throws SQLException {
		skriveTilDb std = new skriveTilDb();
		std.sampleWell(new Well());
		//System.out.println("did it");
		//java.awt.EventQueue.invokeLater(new Runnable() {
			//public void run() {
				//try {
					//registrerWell(null);
				//} catch (SQLException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				//}
			//}
		//});
}
}
